module('TopArenaSC', package.seeall)
TopOrderList = {}
TopOrderList[8] = {1,4,2,3}
TopOrderList[4] = {1,2}
TopOrderList[2] = {1}

local get32Group = function(groupIndex, maxGroup)
    return {groupIndex,maxGroup/2+groupIndex,maxGroup/2-groupIndex+1,maxGroup-groupIndex+1}
end

local get16Group = function(groupIndex, maxGroup)
    return {groupIndex,maxGroup-groupIndex+1}
end

local get8Group = function(groupIndex)
    return {groupIndex}
end

local TopGroupOrder = {}
TopGroupOrder[2] = {}
TopGroupOrder[3] = {}
TopGroupOrder[4] = {}

for i = 1,8 do
    TopGroupOrder[2][i] = get32Group(i,32)
    TopGroupOrder[3][i] = get16Group(i,16)
    TopGroupOrder[4][i] = get8Group(i,8)
end

function InitData(data,isCompetition,myUserId)
    local dataList = {}
    if isCompetition then
        local myGroup = 0
        local winnerGroup = {}
        local tempList = {}
        for _,_data in ipairs(data) do
            if _data.round ~= 5 then
                --规律:groupid - 1 后转二进制取后4位（十进制中就是16的余数），如果 > 7取反，这样出来的值是8页中这条数据的页数
                local temp = (_data.groupId - 1) % 16
                if temp > 7 then
                    temp = 15 - temp
                end
                local page =  temp + 1
                if tempList[page] == nil then
                    tempList[page] = {}
                end
                if myGroup == 0 and  _data.userId == myUserId then
                    myGroup = page
                end
                table.insert(tempList[page],_data)
            else
                table.insert(winnerGroup,_data)
            end
        end

        for i = 1,8 do
            local res = {}
            for _,_data in pairs(tempList[i]) do
                local _index
                for temp = 1,4 do
                    if TopGroupOrder[_data.round][i][temp] == _data.groupId then
                        _index = temp
                    end
                end

                if res[_data.round] == nil then
                    res[_data.round] = {}
                end
                if res[_data.round][_index] == nil then
                    res[_data.round][_index] = {}
                end
                table.insert(res[_data.round][_index],_data)
            end
            dataList[i] = res
        end

        if #winnerGroup > 0 then
            for _,data in pairs(winnerGroup) do
                local user = data.userId
                for _,list in pairs(dataList) do
                    if list[4][1][1].userId == user or list[4][1][2].userId == user then
                        list[5] = {}
                        list[5][1] = {}
                        table.insert(list[5][1],data)
                    end
                end
            end
        end
        return dataList,myGroup
    else
        for _,_data in ipairs(data) do
            if dataList[_data.round] == nil then
                dataList[_data.round] = {}
            end
            if dataList[_data.round][_data.groupId] == nil then
                dataList[_data.round][_data.groupId] = {}
            end
            table.insert(dataList[_data.round][_data.groupId],_data)
        end
        return dataList
    end
end

function GetPlayerDataList(userCount,dataList,dataListBefore)
    --if dataListBefore then
    --    print("============== before ================")
    --    for round,roundData in pairs(dataListBefore) do
    --        print("Round",round)
    --        for group,groupData in pairs(roundData) do
    --            print("Group",group)
    --            for _,data in pairs(groupData) do
    --                print("People: ",tostring(data))
    --            end
    --        end
    --    end
    --end
    --print("============== Now ================")
    --for round,roundData in pairs(dataList) do
    --    print("Round",round)
    --    for group,groupData in pairs(roundData) do
    --        print("Group",group)
    --        for _,data in pairs(groupData) do
    --            print("People: ",tostring(data))
    --        end
    --    end
    --end
    local playerList = {}
    for i = 1,#TopOrderList[userCount] do
        if dataListBefore then
            local num = TopOrderList[userCount][i]
            local num2 = TopOrderList[userCount * 2][i*2 - 1]
            playerList[2*i - 1] = (dataList[num][1] == dataListBefore[num2][1] or dataList[num][1] == dataListBefore[num2][2]) and  dataList[num][2] or dataList[num][1]
            playerList[2*i] = dataList[num][2] == playerList[2*i - 1] and dataList[num][1] or dataList[num][2]
        else
            local num = TopOrderList[userCount][i]
            playerList[2*i - 1] = dataList[num][1]
            playerList[2*i] = dataList[num][2]
        end
    end
    --print("============== End ================")
    return playerList
end

function GetReportIndexGroup(dataList)
    local res = {}
    for _,data in pairs(dataList) do
        if not table.contain_value(res,data.groupId) then
            table.insert(res,data.groupId)
        end
    end
    return res
end

function GetChampionRes(playerDataListBefore,playerDataListAfter)
    --调试用代码，修复，勿删
    --print("============== Before ================")
    --for index,data in pairs(playerDataListBefore) do
    --    print(index,tostring(data))
    --end
    --print("============== After ================")
    --for index,data in pairs(playerDataListAfter) do
    --    print(index,tostring(data))
    --end
    --print("============== End ================")
    local res = {}
    local lastUserList = {}
    for _,userData in pairs(playerDataListAfter) do
        for index,data in pairs(playerDataListBefore) do
            if data.userId == userData.userId then
                local game = math.ceil(index / 2)
                res[game] = {}
                res[game].res = index % 2 == 0 and 2 or 1
                res[game].groupId = data.groupId
                lastUserList[game] = data
                break
            end
        end
    end
    return res,lastUserList
end

function TestDataDebug(dataList,num)
    if num == 1 then
        for group,groupData in pairs(dataList) do
            print("Group",group)
            for _,data in pairs(groupData) do
                print("People: ",tostring(data))
            end
        end
    else
        for round,roundData in pairs(dataList) do
            print("Round",round)
            for group,groupData in pairs(roundData) do
                print("Group",group)
                for _,data in pairs(groupData) do
                    print("People: ",tostring(data))
                end
            end
        end
    end
end